const loginByEmailUrl = 'http://localhost/login/email';
const loginByUsernameUrl = 'http://localhost/login/username';
const registerByEmailUrl = 'http://localhost/register/email'
const identityIdUrl = 'http://localhost:8080/identify/id'

var header = {
    headers: {
        Authorization: localStorage.getItem("Authorization")
    }
};

getLoginVO();

function updateHeader() {
    header = {
        headers: {
            Authorization: localStorage.getItem("Authorization")
        }
    };
}

function getIdentityId() {
    const username = localStorage.getItem("LoginUser");
    const url = identityIdUrl + "?username=" + username;
    axios.get(url, header)
        .then((response) => {
            if(response.data.code === 0) {
                localStorage.setItem("userId", response.data.data)
                let identifyBoxButton = document.getElementById("identify_box");
                identifyBoxButton.disabled = true;
                identifyBoxButton.innerHTML = "已认证";
            } else {
            }
        }).catch((err)=>{

        });
}

function getLoginVO() {
    const username = localStorage.getItem("LoginUser");
    const loginInfoUrl = 'http://localhost/login/vo?username=' + username;
    updateHeader();
    axios.get(loginInfoUrl
        , header).then((response) => {
        const nickname = response.data.data.nickname;
        const avatar = response.data.data.avatar;
        if (avatar != null) {
            document.getElementById("user_avatar_box").style.display = "block"
        }
        if (nickname != null) {
            document.getElementById("username_box").style.display = "block"
            document.getElementById("usernameInfo").innerText = nickname
            document.getElementById("login_box").style.display = "none"
            document.getElementById("reg_box").style.display = "none"
            document.getElementById("logout_box").style.display = "block"
            document.getElementById("identify_box").style.display = "block"
            loginModal.style.display = 'none';
            registerModal.style.display = 'none';
        }
        getIdentityId()
    }).catch((error) => {
        if (error.response && error.response.status === 401) {
            console.log(error.response.data.data);
        }
    })
}

function login() {
    const param = document.getElementById("usr").value;
    const password = document.getElementById("pwd").value;
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (emailRegex.test(param)) {
        loginByEmail(param, password);
    } else {
        loginByUsername(param, password);
    }
}

function logout() {
    const param = localStorage.getItem("LoginUser");
    const logoutUrl = "http://localhost/login/out?username=" + param;
    updateHeader();
    axios.post(logoutUrl, null, header)
        .then((response) => {
            localStorage.removeItem("LoginUser");
            localStorage.removeItem("Authorization");
            localStorage.removeItem("userId");
            document.getElementById("usernameInfo").innerText = ""
            window.location.href = "../html/index.html"
        })
        .catch((err) => {
        });
}

function loginByEmail(email, password) {
    axios.post(loginByEmailUrl, {
        email: email, // 用户名
        password: password  // 密码
    })
        .then((response) => {
            const code = response.data.code;
            if (code === 0) {
                const token = response.data.data.token;
                const avatar = response.data.data.avatar;
                const nickname = response.data.data.nickname;
                const username = response.data.data.username;

                localStorage.setItem("LoginUser", username);
                localStorage.setItem("Authorization", token);
                getLoginVO();
            } else {
                document.getElementById("loginErr").hidden = false
                document.getElementById("loginErr").innerText = response.data.msg
            }
        })
        .catch((err) => {
            if (err.response) {
                document.getElementById("loginErr").hidden = false
                document.getElementById("loginErr").innerText = err.data.msg
            }
        });
}

function loginByUsername(username, password) {
    axios.post(loginByUsernameUrl, {
        username: username, // 用户名
        password: password  // 密码
    })
        .then((response) => {
            const code = response.data.code;
            if (code === 0) {
                const token = response.data.data.token;
                const avatar = response.data.data.avatar;
                const nickname = response.data.data.nickname;
                const username = response.data.data.username;

                localStorage.setItem("LoginUser", username);
                localStorage.setItem("Authorization", token);
                getLoginVO();
            } else {
                document.getElementById("loginErr").hidden = false
                document.getElementById("loginErr").innerText = response.data.msg
            }
        })
        .catch((err) => {
            if (err.response) {
                document.getElementById("loginErr").hidden = false
                document.getElementById("loginErr").innerText = err.data.msg
            }
        });
}

function registerByEmail() {
    const username = document.getElementById("usrReg").value;
    const password = document.getElementById("pwdReg").value;
    const email = document.getElementById("emailReg").value;
    const smsCode = document.getElementById("codeReg").value;
    axios.post(registerByEmailUrl,
        {
            username: username,
            password: password,
            email: email,
            smsCode: smsCode
        })
        .then((response) => {
            const code = response.data.code;
            if (code === 0) {
                const token = response.data.data.token;
                const avatar = response.data.data.avatar;
                const nickname = response.data.data.nickname;
                const username = response.data.data.username;

                localStorage.setItem("LoginUser", username);
                localStorage.setItem("Authorization", token);
                getLoginVO();
            } else {
                document.getElementById("regErr").hidden = false
                document.getElementById("regErr").innerText = response.data.msg
            }
        })
        .catch((err) => {
            document.getElementById("regErr").hidden = false
            document.getElementById("regErr").innerText = err.data.msg
        });
}

function sendSMSCode() {
    const email = document.getElementById("emailReg").value;
    const sendSMSCodeURL = 'http://localhost/register/email/' + email;
    axios.get(sendSMSCodeURL)
        .then((response) => {
        })
        .catch((err) => {
        });
}
